//
//  XTabBar.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/8.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import UIKit

//*********  step (3 创建自定义bar)
class XTabBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
    }

    //*********  step (4 重写layousubview 此方法用来重新布局安妮)
    override func layoutSubviews() {
        let btnCount = 5;
        let w = self.bounds.size.width / CGFloat(btnCount)
        let h = self.bounds.size.height
        //按钮frame的基值
        let frame = CGRect(x: 0, y: 0, width: w, height: h)
        var index = 0
        for v in subviews
        {
            if v is UIControl && !(v is UIButton)
            {
                v.frame = frame.offsetBy(dx: CGFloat(index) * w, dy: 0)
                index += 1
            }
            if (index == 2)
            {
                //*********  step (5 给加号按钮留一个位子)
                index += 1
            }
        }
        
        //*********  step (7 加载加号按钮 MainViewController)
        self.composedButton.frame = frame
        self.composedButton.center = CGPoint(x: self.center.x, y: h*0.5)
        
        
    }
    
    //*********  step (6 懒加载加号按钮)
    lazy var composedButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControlState())
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState())
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.selected)
        
        self.addSubview(btn)
        
        return btn
        
    }()
    
}
